package com.sdy.interceptor;

import com.sdy.util.Contact;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 如果是我公司的管理员，需要选择查看的客户才能进入页面
 */
public class ChooseCompanyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        System.out.println("ChooseCompany拦截器生效了");
        System.out.println("ChooseCompany拦截器中:" + "CCI_"+request.getRequestURI()+"_pass:" + request.getSession().getAttribute("CCI_"+request.getRequestURI()+"_pass"));
        if ((Boolean)request.getSession().getAttribute("isManager") && !(Boolean)request.getSession().getAttribute("CCI_"+request.getRequestURI()+"_pass")){
            try {
                response.sendRedirect("/company/choose");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.out.println("ChooseCompany拦截器结束了");
        request.getSession().setAttribute("CCI_"+request.getRequestURI()+"_pass",false);
        request.getSession().removeAttribute(Contact.COMPANY_KET);
    }
}
